/**
 * 
 */
package view.impl.ingame;

import java.io.FileNotFoundException;

import model.IinGame;
import model.ModelFactory;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;

import util.ResLoc;
import view.impl.FontContainer;
import view.impl.menu.CascadedMenu;
import view.impl.menu.CascadedMenuLayer;
import view.impl.menu.items.MenuButton;


/**
 * @author Julian
 *
 */
public class GameHUD {
	private StatusMeter p1Meter = null;
	private StatusMeter p2Meter = null;
	
	private Image menuBackground = null;
	
	private CascadedMenu menu = null;
	
	UnicodeFont uFont = FontContainer.getMenuFont();
	
	public GameHUD(Fighter player1, String p1Nick, String p1Char, Fighter player2, String p2Nick, String p2Char) throws SlickException, FileNotFoundException {
		p1Meter = new StatusMeter(p1Char + " (" + p1Nick + ")", player1, false);
		p2Meter = new StatusMeter(p2Char + " (" + p2Nick + ")", player2, true);
		
		menuBackground = new Image(ResLoc.getResource("InGame.menuBackground"));
		
		initMenu();
	}
	
	private void initMenu() throws SlickException, FileNotFoundException {
		UnicodeFont uFont = FontContainer.getInGameMenuFont();
		
		// TODO: Load from file?
		
			CascadedMenuLayer offensive = new CascadedMenuLayer();
			offensive.addMenuItem(new MenuButton("Strong", uFont, 0, offensive.getHeight()), 0);
			offensive.addMenuItem(new MenuButton("Fast", uFont, 0, offensive.getHeight()), 1);
			offensive.addMenuItem(new MenuButton("Precise", uFont, 0, offensive.getHeight()), 2);
			
			CascadedMenuLayer ranged = new CascadedMenuLayer();
			ranged.addMenuItem(new MenuButton("Strong", uFont, 0, ranged.getHeight()), 0);
			ranged.addMenuItem(new MenuButton("Fast", uFont, 0, ranged.getHeight()), 1);
			ranged.addMenuItem(new MenuButton("Precise", uFont, 0, ranged.getHeight()), 2);
			
			CascadedMenuLayer defensive = new CascadedMenuLayer();
			defensive.addMenuItem(new MenuButton("Block", uFont, 0, defensive.getHeight()), 0);
			defensive.addMenuItem(new MenuButton("Dodge", uFont, 0, defensive.getHeight()), 1);
			defensive.addMenuItem(new MenuButton("Counter", uFont, 0, defensive.getHeight()), 2);
			
			CascadedMenuLayer strategic = new CascadedMenuLayer();
			strategic.addMenuItem(new MenuButton("Charge", uFont, 0, strategic.getHeight()), 0);
			strategic.addMenuItem(new MenuButton("Retreat", uFont, 0, strategic.getHeight()), 1);
		
		CascadedMenuLayer physical = new CascadedMenuLayer();
		physical.addMenuItem(new MenuButton("Offensive", uFont, 0, physical.getHeight()), 0, offensive);
		physical.addMenuItem(new MenuButton("Ranged", uFont, 0, physical.getHeight()), 1, ranged);
		physical.addMenuItem(new MenuButton("Defensive", uFont, 0, physical.getHeight()), 2, defensive);
		physical.addMenuItem(new MenuButton("Strategic", uFont, 0, physical.getHeight()), 3, strategic);
		
			CascadedMenuLayer elemental = new CascadedMenuLayer();
			elemental.addMenuItem(new MenuButton("Fire", uFont, 0, elemental.getHeight()), 0);
			elemental.addMenuItem(new MenuButton("Water", uFont, 0, elemental.getHeight()), 1);
			elemental.addMenuItem(new MenuButton("Wind", uFont, 0, elemental.getHeight()), 2);
			elemental.addMenuItem(new MenuButton("Wood", uFont, 0, elemental.getHeight()), 3);
			
		CascadedMenuLayer magical = new CascadedMenuLayer();
		magical.addMenuItem(new MenuButton("Elemental", uFont, 0, magical.getHeight()), 0, elemental);
		
			CascadedMenuLayer enhanced = new CascadedMenuLayer();
			enhanced.addMenuItem(new MenuButton("Strong", uFont, 0, enhanced.getHeight()), 0);
			enhanced.addMenuItem(new MenuButton("Fast", uFont, 0, enhanced.getHeight()), 1);
			enhanced.addMenuItem(new MenuButton("Precise", uFont, 0, enhanced.getHeight()), 2);
			
		CascadedMenuLayer combined = new CascadedMenuLayer();
		combined.addMenuItem(new MenuButton("Combined", uFont, 0, combined.getHeight()), 0, enhanced);	
		
		CascadedMenuLayer entry = new CascadedMenuLayer();
		entry.addMenuItem(new MenuButton("Physical", uFont, 0, entry.getHeight()), 0, physical);
		entry.addMenuItem(new MenuButton("Magical", uFont, 0, entry.getHeight()), 1, magical);
		entry.addMenuItem(new MenuButton("Combined", uFont, 0, entry.getHeight()), 2, combined);
		entry.addMenuItem(new MenuButton("Exit", uFont, 0, entry.getHeight()), 3);
		
		menu = new CascadedMenu();
		menu.setEntryLayer(entry);
		
	}
	
	public void draw(Graphics g, int windowWidth, int windowHeight) {
		float scaleX = (float)windowWidth / 800f;
		float scaleY = (float)windowHeight / 600f;
		
		IinGame model = ModelFactory.getViewModel().getInGameState();
		
		
		p1Meter.draw(windowWidth, windowHeight);
		p2Meter.draw(windowWidth, windowHeight);
		
		menuBackground.draw(0, 400 * scaleY, 800 * scaleX, 200 * scaleY);
		
		int[] menuChoices = model.getMarkedMenuItem();
		menu.draw(menuChoices, g, windowWidth, windowHeight);
		
		// Draw results!
		// TODO: Change font!
//		BattleEvent result = model.getPlayerOneAction();
//		String p1Nick = model.getPlayerOneNick();
//		String p2Nick = model.getPlayerTwoNick();
		int textPosY = 150;
		uFont.drawString(400 * scaleX - uFont.getWidth(model.getMessage()) / 2f, textPosY * scaleY, model.getMessage(), new Color(255, 185, 25));
		/*
		if(result == BattleEvent.WIN) {
			uFont.drawString(400 * scaleX - uFont.getWidth(p1Nick + " WINS!") / 2, textPosY * scaleY, p1Nick + " WINS!", new Color(255, 185, 25));
		} else if (result == BattleEvent.LOSE) {
			uFont.drawString(400 * scaleX - uFont.getWidth(p2Nick + " WINS!") / 2, textPosY * scaleY, p2Nick + " WINS!", new Color(255, 185, 25));
		} else if(result == BattleEvent.DRAW) {
			uFont.drawString(400 * scaleX - uFont.getWidth("DRAW!") / 2, textPosY * scaleY, "DRAW!", new Color(255, 185, 25));
		}
		*/
	}
}
